package com.netease.nim.uikit.net.rxjava;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.netease.nim.uikit.net.entity.BaseBean;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * 请求接口都走此处：易涛xxx
 */
class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final Type type;
    private Context context;

    GsonResponseBodyConverter(Gson gson, Type type) {
        this.gson = gson;
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
        BaseBean httpResult = gson.fromJson(response, BaseBean.class);
        Log.i("=====Msg=======", httpResult.getMsg());
        if (!httpResult.isSuccess()) {
            if(httpResult.getMsg().contains("余额不足")){
                return gson.fromJson(response, type);
            }else if(httpResult.getMsg().contains("noWeChatOpenId")){
                return gson.fromJson(response, type);
            }else if(httpResult.getMsg().contains("请维护支付宝提现账号！")){
                return gson.fromJson(response, type);
            }else{
                throw new ApiException(httpResult);
            }
        }
        return gson.fromJson(response, type);
    }
}
